package cn.green.core.excel.model.impls;

import cn.green.core.excel.model.Column;
import cn.green.core.model.QField;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
//import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wang.yishu
 * @version 1.0
 * @className NumberColumn
 * @description 数字单元格
 * @date 2025/1/6 10:24
 */
public class NumberColumn extends Column {

    public NumberColumn(Integer index, QField field) {
        super(index, field);
    }

    @Override
    public List<HSSFCell> generateXlsTitle(HSSFWorkbook workbook, HSSFSheet sheet, HSSFRow row) {
        HSSFCell cell = row.createCell(this.index);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        if (this.xlsTitleCellStyle != null) {
            cell.setCellStyle(this.xlsTitleCellStyle);
        }
        cell.setCellValue(this.columnName);
        return Collections.singletonList(cell);
    }

    @Override
    public List<XSSFCell> generateXlsxTitle(XSSFWorkbook workbook, XSSFSheet sheet, XSSFRow row) {
        XSSFCell cell = row.createCell(this.index);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        if (this.xlsxTitleCellStyle != null) {
            cell.setCellStyle(this.xlsxTitleCellStyle);
        }
        cell.setCellValue(this.columnName);
        return Collections.singletonList(cell);
    }

    @Override
    public List<HSSFCell> generateXlsCell(HSSFWorkbook workbook, HSSFSheet sheet, HSSFRow row, Object cellVal) {
        HSSFCell cell = row.createCell(this.index);
        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        if (cellVal != null) {
            cell.setCellValue(Double.parseDouble(cellVal.toString()));
        }
        return Collections.singletonList(cell);
    }

    @Override
    public List<XSSFCell> generateXlsxCell(XSSFWorkbook workbook, XSSFSheet sheet, XSSFRow row, Object cellVal) {
        XSSFCell cell = row.createCell(this.index);
        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        if (cellVal != null) {
            cell.setCellValue(Double.parseDouble(cellVal.toString()));
        }
        return Collections.singletonList(cell);
    }

    @Override
    public Map<String, Object> getValueFromXlsCell(Map<String, HSSFCell> cells) {
        Map<String, Object> data = new HashMap<>();
        cells.forEach((fieldName, cell) -> {
            int cellType = cell.getCellType();
            if (HSSFCell.CELL_TYPE_NUMERIC == cellType) {
                 data.put(fieldName, cell.getNumericCellValue());
            } else if (HSSFCell.CELL_TYPE_STRING == cellType) {
                String cellValue = cell.getStringCellValue();
                if (cellValue != null && !cellValue.isEmpty()) {
                    data.put(fieldName, Double.valueOf(cellValue));
                }
            }
        });

        return data;
    }

    @Override
    public Map<String, Object> getValueFromXlsxCell(Map<String, XSSFCell> cells) {
        Map<String, Object> data = new HashMap<>();
        cells.forEach((fieldName, cell) -> {
            int cellType = cell.getCellType();
            if (HSSFCell.CELL_TYPE_NUMERIC == cellType) {
                data.put(fieldName, cell.getNumericCellValue());
            } else if (HSSFCell.CELL_TYPE_STRING == cellType) {
                String cellValue = cell.getStringCellValue();
                if (cellValue != null && !cellValue.isEmpty()) {
                    data.put(fieldName, Double.valueOf(cellValue));
                }
            }
        });
        return data;
    }
}
